package com.unittec.zk.provider.exam.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.unittec.zk.provider.exam.entity.Attachment;
import com.unittec.zk.provider.exam.enums.NoticeStateEnum;

import com.unittec.zk.provider.utils.CopyUtils;
import com.unittec.zk.provider.utils.UUIDUtils;
import com.unittec.zk.sdk.exam.domain.AttachmentDTO;
import com.unittec.zk.sdk.exam.request.*;
import org.springframework.stereotype.Service;
import com.unittec.zk.provider.exam.entity.NoticeInfo;
import com.unittec.zk.provider.exam.mapper.NoticeInfoMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NoticeInfoService extends ServiceImpl<NoticeInfoMapper, NoticeInfo> {

    @Resource
    private AttachmentService attachmentService;



    /**
     * 公告信息 分页
     *
     * @param req201052
     * @return Object
     */
    public Object getNoticePage(Req201052 req201052) {
        //加载一个条件构造器
        LambdaQueryWrapper<NoticeInfo> qw = new LambdaQueryWrapper<>();


        //如果‘名称’参数不为空，则将其添加到条件构造器中
        if (StrUtil.isNotBlank(req201052.getNoticeTitle())) {
            qw.apply("notice_title like '%"+req201052.getNoticeTitle()+"%'");
        }

        if (StrUtil.isNotBlank(req201052.getNoticeState())) {
            qw.apply("notice_state = '"+req201052.getNoticeState()+"'");
        }

        if (req201052.getCreateStartDate()!=null) {
            qw.apply("create_time >= "+req201052.getCreateStartDate());
        }

        if (req201052.getCreateEndDate()!=null) {
            qw.apply("create_time <= "+req201052.getCreateEndDate());
        }

        qw.apply("del_flag='0' ");
        System.out.println("select sql:  "+qw.getSqlSelect());


        //根据条件构造器和分页参数，去查询数据库
        Page<Map<String, Object>> page = this.baseMapper.getNoticePage(new Page<Object>(req201052.getCurrent(), req201052.getSize()), qw);

        //返回结果
        return page;
    }
    /**
     * 公告信息-批量删
     * @param req201053
     * @return
     */
    public boolean delNoticeByIds(Req201053 req201053) {
        boolean delFlag=false;
        List<NoticeInfo> list = new ArrayList<NoticeInfo>(15);
        for (String id : req201053.getIds()) {
            NoticeInfo noticeInfo = new NoticeInfo();
            noticeInfo.setId(id);
            noticeInfo.setDelFlag("1");
            list.add(noticeInfo);

        }
        if (list.size() > 0) {
            delFlag= this.updateBatchById(list);
        }
        return delFlag;
    }


    /**
     * 公告信息-批量撤回
     * @param req201054
     * @return
     */
    public boolean withdrawNoticeByIds(Req201054 req201054) {
        boolean delFlag=false;
        List<NoticeInfo> list = new ArrayList<NoticeInfo>(15);
        for (String id : req201054.getIds()) {
            NoticeInfo noticeInfo = new NoticeInfo();
            noticeInfo.setId(id);
            noticeInfo.setNoticeState(NoticeStateEnum.NOTICE_STATE_TOBEPUBLISHED.getCode());
            list.add(noticeInfo);

        }
        if (list.size() > 0) {
            delFlag= this.updateBatchById(list);
        }
        return delFlag;
    }


    /**
     * 公告信息-查询详情
     * @param req201055
     * @return
     */
    public Object getNoticeById(Req201055 req201055) {
        //根据id查询记录
        NoticeInfo noticeInfo = this.getById(req201055.getId());

        LambdaQueryWrapper<Attachment> qw = new LambdaQueryWrapper<>();
        qw.eq(Attachment::getRelationId,req201055.getId());
        List<Attachment> list = attachmentService.list(qw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("notice",noticeInfo);
        map.put("attachment_list",list);
        //返回结果
        return map;
    }



    /**
     * 公告信息-增/改
     *
     * @param req201056
     * @return
     */
    public Object saveOrUpdateNotice(Req201056 req201056) {
        NoticeInfo noticeInfo = new NoticeInfo();
        CopyUtils.copyProperties(req201056,noticeInfo);
        if (StrUtil.isBlank(noticeInfo.getId())) {
            noticeInfo.setId(UUIDUtils.getId());
        }
        if (StrUtil.isBlank(noticeInfo.getNoticeState())) {
            noticeInfo.setNoticeState("1");
        }
        if (StrUtil.isBlank(noticeInfo.getDelFlag())) {
            noticeInfo.setDelFlag("0");
        }
        //存notice
        this.saveOrUpdate(noticeInfo);

        //存附件关联关系
          //1、删除原附件关联关系
        LambdaQueryWrapper<Attachment> qw = new LambdaQueryWrapper<>();
        qw.eq(Attachment::getRelationId, noticeInfo.getId());
        attachmentService.remove(qw);
          //2、存新附件关联关系
        List<AttachmentDTO> fileDtoList = req201056.getFileDtoList();
        ArrayList<Attachment> attachments = new ArrayList<>();
        for (AttachmentDTO attachmentDTO : fileDtoList) {
            Attachment attachment = new Attachment();
            CopyUtils.copyProperties(attachmentDTO, attachment);
            //给每个附件设置关联的记录id
            attachment.setRelationId(noticeInfo.getId());
            attachments.add(attachment);
        }
        attachmentService.saveBatch(attachments);

        return "Success";
    }



}
